<?php
include_once("./configuration.php");
/* TODO: Lots of finnish in this document */
// This function is used to check the language.
function checkLanguage(){
	
	if(isset($_SESSION['logged']) && $_SESSION['logged'] === true){
		if($_SESSION['language'] == 'default'){
			return ("fi");
		}
		else{
			return($_SESSION['language']);
		}
	}else{
		return("en");
	}
}
// This function prints the highlighted variables in the edit-view.
function printHighlightVariables(){
	if(isset($_GET['file'])) {			
		 $file = $_GET['file'].".html";
		 $tmpfile = "tmp/".rand(0,1000000).".html";
		 copy($_SESSION['processed'].$file, $tmpfile);
		 $folderpath = $tmpfile;
	}
	if(isset($_GET['file']) && file_exists($folderpath)){
		$file = fopen($folderpath, 'r');
		$haystack = fread($file, filesize($folderpath));
		fclose($file);
		$tobeappended = "";
		preg_match_all("/group='([^']*)/", $haystack, $m);
		$table = $m[1];
		$table = array_unique($table);
		$colorcode = 103;
		$coloriterator = 0;
		foreach($table as $t) {
		  $r = rand(0,255); $g = rand(0,255); $b = rand(0,255);
		  while(( $r + $g + $b )< 420) {
		    $r=($r+rand(0,127)) % 150+80; $g =($g+rand(0,127))%150+80;$b= ($b+rand(0,127)) % 150+80;
                              }
		  $tobeappended .= "[group=\"".$t."\"]{background:rgb(".$r.",".$g.",".$b.");}\n";
		  $colorcode += 35;
		  $coloriterator++;
		}
		echo '<style type="text/css">'.$tobeappended.'</style>';
	}
}
// This creates the filetable.
function printFileTable(){
	$dir = $_SESSION['uploads'];
	$dir2 = $_SESSION['processed'];
	$dir3 = $_SESSION['completed'];
	$list = scandir("$dir");
	global $lang_lib;
	global $userdirs_relative;

	/* Creates tablecells from the filenames and highlights the columns which already have been processed */

	for ($i = 2; $i < count($list); $i++) {
		$filename = $list[$i];
		$filename = explode(".", $filename);$type=$filename[sizeof($filename)-1]; 
		 unset($filename[sizeof($filename)-1]); $filename=implode(".", $filename);
		$muokkausaika = date ("Y/m/d H:i", filemtime($dir.$filename.".".$type));
		$size = round(filesize("$dir$filename.$type"), 0, PHP_ROUND_HALF_EVEN);
		$string = "<tr class=";
		if(file_exists($dir3.$filename.".html")){
			$string = $string."\"valmissarake\"><td>".$muokkausaika."</td><td name=\"nimi".$i."\"><span id=\"orderBy\">".$filename.".".$type."</span> (".$size." tavua)";
			$string = $string."<a href=\"?p=edit&file=".$filename.".".$type."\"><img class=\"kuva\" src=\"graphics/button_edit_grey.gif\" alt=\"Editoi tiedostoa\"></a>";
			$string = $string."<a href=\"".$userdirs_relative.$_SESSION['user']."/completed/".$filename.".pdf\"><img class=\"kuva\" src=\"graphics/pdficon_large.gif\" alt=\"Näytä tiedosto\"></a>";
			$string = $string."<br /><span class=\"status\">".$lang_lib["status"].": <span class=\"status_completed\">".$lang_lib["completed"]."</span></span>";
		} else if(file_exists($dir2.$filename.".".$type.".html")){
			$string = $string."\"korostettusarake\"><td>".$muokkausaika."</td><td name=\"nimi".$i."\"><span id=\"orderBy\">".$filename.".".$type."</span> (".$size." tavua)";
			$string = $string."<a href=\"?p=edit&file=".$filename.".".$type."\"><img class=\"kuva\" src=\"graphics/button_edit_grey.gif\" alt=\"Editoi tiedostoa\"></a>";
			$string = $string."<br /><span class=\"status\">".$lang_lib["status"].": <span class=\"status_processed\">".$lang_lib["processed"]."</span></span>";
		}
		else{
			$string = $string."\"sarake\"><td>".$muokkausaika."</td><td name=\"nimi".$i."\"><span id=\"orderBy\">".$filename.".".$type."</span> (".$size." tavua)";
			$string = $string."<br /><span class=\"status\">".$lang_lib["status"].": <span class=\"status_unprocessed\">".$lang_lib["unprocessed"]."</span></span>";
		}
		// TODO: "arvo" is finnish for "value" and very stupid variable name
		$string = $string."</td><td><input type=\"checkbox\" name=\"testiboksi".$i."\"></td><input type=\"hidden\" name=\"arvo".$i."\" value=\"".$filename.".".$type."\"/></tr>";
		echo $string;
	}
}




?>
